package tm;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 数据库链接数据池
 * @author wsz
 * @date 2021/10/13 14:13
 **/
public class DruidUtils {

    private DataSource dataSource;

    private DruidUtils() throws PropertyVetoException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
        comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC");
        comboPooledDataSource.setUser("root");
        comboPooledDataSource.setPassword("");

        this.dataSource = comboPooledDataSource;
    }

    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("cannot get connection");
    }



    public static DruidUtils getInstance() {
        DruidUtils utils = null;
        try {
            utils = new DruidUtils();
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

        return utils;
    }
}
